<?php


namespace app\admin\controller\goods;


use app\common\controller\Adminbase;
use think\Db;

class Egoods extends Adminbase
{
    //资产标注袋审核页面
    protected $searchFields = 'name';

    protected function initialize()
    {
        parent::initialize();
    }
    public function getc_cate()
    {
        $id = input('id');
        $query = db('area')->where('pid', $id)->select();
        return json(['code' => 200, 'data' => $query, 'msg' => '获取成功']);
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            $page = input('page/d', 1);
            $limit = input('limit/d', 10);
            $filter = input('filter');
            //待审核  和 审核失败状态
            $where[] = ['g.status','in',[2,3]];
            $where[] = ['g.is_del','=',0];
            if ($filter) {
                $filter = json_decode($filter, true);
                if (isset($filter['business_name'])) {
                    $where[] = ['b.business_name', 'like',  '%'.$filter['business_name'].'%' ];
                }
                if (isset($filter['name'])) {
                    $where[] = ['g.name', 'like',  '%'.$filter['name'].'%' ];
                }
                if (isset($filter['province'])) {
                    $province = Db::name('area')->find($filter['province']);
                    $where[] = ['b.province', '=', $province['name']];
                }
                if (isset($filter['city'])) {
                    $city = Db::name('area')->find($filter['city']);
                    $where[] = ['b.city', '=', $city['name']];
                }
            }
            $search = input('search');

            $_list = Db::name('goods')
                ->alias('g')
                ->where($where)
                ->field('g.*,
                b.business_name,
                b.name as people_name,
                b.phone,
                b.province,
                b.city,
                b.area,
                b.address')
                ->join('business b', 'g.business_id=b.id')
                ->page($page, $limit)
                ->order('create_time desc')
                ->select();
            foreach ($_list as $k => $v) {
                $_list[$k]['address_detail'] = $v['province'] . $v['city'] . $v['area'] . $v['address'];
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
            }
            $total = Db::name('goods')
                ->alias('g')
                ->join('business b', 'g.business_id=b.id')
                ->where($where)
                ->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        $area_list=Db::name('area')->where([
            ['id','<>',0],
            ['pid','=',0]
        ])->select();
        foreach ($area_list as $k=>$v){
            $proviec[$v['id']]=$v['name'];
        }
        $this->assign('proviec',$proviec);
        return $this->fetch();
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            //verify_time 审核通过时间
            //status 1已上架 2审核中 3未通过 4仓库中
            $insert_data = array(
                'id' => $data['id'],
                'status' => $data['row']['status']
            );
            if ($insert_data['status']==1){
                $insert_data['verify_time']=time();
            }
            $business_id = Db::name('goods')
                ->update($insert_data);
            $this->success('操作成功！');
        } else {
            $id = $this->request->param('id/d', 0);

            $data = Db::name('goods')->find($id);
            $cate_ids = explode(',', $data['cate_id']);
            $cate_names = Db::name('operate_cate')->where('id', 'in', $cate_ids)->column('name');
            $data['cate_name'] = implode(',', $cate_names);
            $data['purchase_time'] = $data['purchase_time_start'] . '——' . $data['purchase_time_end'];
            $data['qu_time'] = $data['qu_time_start'] . '——' . $data['qu_time_end'];
            $data['purchase_time_ming'] = $data['purchase_time_start_ming'] . '——' . $data['purchase_time_end_ming'];
            $data['qu_time_ming'] = $data['qu_time_start_ming'] . '——' . $data['qu_time_end_ming'];
            $this->assign('data', $data);
            return $this->fetch();
        }
    }
}